<script type="text/javascript">
function initAdvForm() {
    $('.selectpicker').selectpicker('refresh');
    laydate.render({elem: '.laydate_input_datetime', type: 'datetime', trigger: 'click'});
    laydate.render({elem: '.laydate_input_date', trigger: 'click'});
}

function deleteAdvQuery(index) {
    adv_details.splice(index, 1);
    IssueAdvQuery.prototype.renderAdvQuery(adv_details);
}

function clearAdvSearchQuery() {
    $("#adv_andor").val("");
    $('#adv_field option:selected').prop("selected", false);
    $("#adv_opt").val("");
    $(".form-control[dataType='" + advQueryType + "']").val("");

    advQueryType = "text";
    advQuerySource = "";
    advQueryTitle = "请选择";
    document.getElementById("adv_andor").options.selectedIndex = 0;
    document.getElementById("adv_field").options.selectedIndex = 0;
    document.getElementById("adv_opt").options.selectedIndex = 0;

    $("#dynamic-form").html(IssueAdvQuery.prototype.renderTableAdvQuery("content", "text", "", "", "值"));
    $('.selectpicker').selectpicker('refresh');
}

$(function () {
    window.$IssueAdvQuery = new IssueAdvQuery(_issueConfig);
    // 高级搜索字段修改
    $(document).on("change", "#adv_field", function () {
        var $selected = $('#adv_field option:selected');
        var dataType = $selected.data("type");
        var dataSource = $selected.data("source");
        var dataTitle = $selected.data("content");
        advQueryType = dataType;
        advQuerySource = dataSource;
        advQueryTitle = dataTitle;

        if (dataTitle === "解决结果") {
            dataSource = "resolve";
        }

        var html = IssueAdvQuery.prototype.renderTableAdvQuery("content", dataType, "", "", dataTitle, dataSource);

        $("#dynamic-form").html(html);

        initAdvForm();
    });

    // 高级搜索
    $(document).on("click", "#btn-adv_add_condition", function (e) {
        var field_title = $('#adv_field option:selected').data("content");
        var $select = $("select[name='content'] option:selected");
        var display_html = $(".form-control[name='content']").val();

        if ($select.length > 0) {
            display_html = $select.data("display_html");
            var avatar = $select.data("avatar");
        }
        let field_name = $('#adv_field').val();
        let opt = $("#adv_opt").val();
        let value = $(".form-control[name='content']").val();

        if (typeof field_name == "undefined" || field_name == null || field_name == "") {
            notify_error("请选择查询字段");
            return false;
        }
        if (typeof opt == "undefined" || opt == null || opt == "") {
            notify_error("请选择操作符");
            return false;
        }

        if (typeof value == "undefined" || value == null || value == "") {
            $(".form-control[name='content']").val("");
        }


        var temp = {
            logic: $("#adv_andor").val() || "and",
            start_braces: "",
            field_title: field_title,
            field: field_name,
            opt: opt,
            data_type: advQueryType,
            data_source: advQuerySource,
            data_title: advQueryTitle,
            value: value,
            avatar: avatar || "",
            display_html: display_html,
            end_braces: ""
        };

        console.log(temp);

        adv_details.push(temp);
        IssueAdvQuery.prototype.renderAdvQuery(adv_details);
        clearAdvSearchQuery();
    });

    // 点击高级查询
    $(document).on("click", "#btn-adv_save", function () {
        var tempData = JSON.parse(JSON.stringify(adv_details));
        var temp = tempData.map(function (n) {
            return {
                logic: n.logic,
                start_braces: n.start_braces,
                field: n.field,
                opt: n.opt,
                value: n.value,
                end_braces: n.end_braces
            }
        });
        var queryData = JSON.stringify(temp);
        var $self = $(this);
        if ($self.hasClass("disabled")) {
            return false;
        }
        $self.addClass('disabled');

        $.ajax({
            type: 'post',
            dataType: "json",
            async: false,
            url: '/issue/main/save_advance_query',
            data: {
                query: queryData
            },
            success: function (resp) {
                auth_check(resp);
                if (resp.ret == '200') {
                    notify_success('操作成功');
                } else {
                    notify_error(resp.msg);
                }
                $self.removeClass('disabled');

            },
            error: function (res) {
                notify_error("请求数据错误" + res);
                $self.removeClass('disabled');
                //$("#modal-adv_query").modal('hide');
            }
        });
    });

    // 清空条件
    $(document).on("click", "#btn-adv_clear", function () {
        adv_details = [];
        IssueAdvQuery.prototype.renderAdvQuery(adv_details);
    });


    // 双击修改
    $(document).on("dblclick", "#adv-query-box .table-text", function () {
        var $self = $(this);
        var dataType = $self.data("type");
        var dataSource = $self.data("source");
        var dataTitle = $self.data("title");
        var dataIndex = $self.parent().parent().data("index");
        var data = $self.data("content");
        var filed_name = $self.data("filed");
        var html = IssueAdvQuery.prototype.renderTableAdvQuery(filed_name, dataType, data, dataIndex, dataTitle, dataSource);

        $self.parent().find(".table-form").html(html).removeClass("hidden");
        $self.addClass("hidden");
        initAdvForm();
    });

    $(document).on("change", "#adv-query-box .table-form select", function () {
        var $self = $(this);
        var index = $self.data("index");
        var field_name = $self.data("name");
        var value = $self.val();
        var $selected = $self.find('option:selected');

        if (field_name === "field") {
            adv_details[index].data_type = $selected.data("type");
            adv_details[index].data_source = $selected.data("source");
            adv_details[index].data_title = $selected.data("title");
        }

        adv_details[index][field_name] = value;

        if (field_name === "value") {
            adv_details[index].display_html = $selected.data("display_html");
        }
        if ($selected.data("avatar")) {
            adv_details[index].avatar = $selected.data("avatar");
        }

        console.log(adv_details[index]);

        $self.parent().html("").addClass("hidden");
        $self.parent().find(".table-text").removeClass("hidden");

        IssueAdvQuery.prototype.renderAdvQuery(adv_details);
    });

    $(document).on("blur", "#adv-query-box .table-form input:not(.laydate_input)", function () {
        var $self = $(this);
        var index = $self.data("index");
        var field_name = $self.data("name");
        var value = $self.val();

        adv_details[index][field_name] = value;
        adv_details[index].display_html = value;

        IssueAdvQuery.prototype.renderAdvQuery(adv_details);
    });


    $(document).on("click", function (e) {
        var $form = $(".table-form");
        if (!$form.is(e.target) && $form.has(e.target).length === 0) {
            $(".table-text").removeClass("hidden");
            $(".table-form").addClass("hidden");

            var $date = $("#adv-query-box .table-form input.laydate_input");

            if ($date.length > 0) {
                $date.each(function () {
                    var $self = $(this);
                    var index = $self.data("index");
                    var field_name = $self.data("name");
                    var value = $self.val();

                    adv_details[index][field_name] = value;
                    IssueAdvQuery.prototype.renderAdvQuery(adv_details);
                });
            }
        }
    });

    $(document).on("click", "#btn_adv_sort_by", function () {
        var sort_by = $("#adv_sort_by").val();
        if (sort_by === "asc") {
            $("#adv-sort-by-asc").removeClass("hidden");
            $("#adv-sort-by-desc").addClass("hidden");
            $("#adv_sort_by").val("desc");
            $(this).prop("title", "升序");
        } else {
            $("#adv-sort-by-asc").addClass("hidden");
            $("#adv-sort-by-desc").removeClass("hidden");
            $("#adv_sort_by").val("asc");
            $(this).prop("title", "降序");
        }
    });

    $('#modal-adv_query').on('shown.bs.modal', function (e) {

        $('.selectpicker').selectpicker('refresh');
        // 拖拽排序
        [].forEach.call(document.getElementById('adv-query-box').getElementsByClassName('adv-tbody'), function (el) {
            Sortable.create(el, {
                group: 'item',
                animation: 150,
                onStart: function (evt) {
                    // 拖拽完毕之后发生该事件
                    // 所在位置
                    var $parent = $(evt.item).parent();
                    var index = $parent.children().index($(evt.item));
                    console.log(evt.item.title + "拖动前位置：", index);
                    sort_adv_temp_item = adv_details[index];
                    adv_details.splice(index, 1);
                },
                onEnd: function (evt) {
                    // 拖拽完毕之后发生该事件
                    // 所在位置
                    var $parent = $(evt.item).parent();
                    var index = $parent.children().index($(evt.item));
                    console.log(evt.item.title + "拖动到位置：", index);

                    adv_details.splice(index, 0, sort_adv_temp_item);
                },
                onUpdate: function (evt) {
                    // 拖拽完毕之后发生该事件
                    // 所在位置
                    var $parent = $(evt.item).parent();
                    var index = $parent.children().index($(evt.item));
                    console.log(evt.item.title + "拖动后位置：", index);
                }
            });
        });
    });

    $('#btn-adv_submit').bind('click', function () {
        var tempData = JSON.parse(JSON.stringify(adv_details));
        var temp = tempData.map(function (n) {
            return {
                logic: n.logic,
                start_braces: n.start_braces,
                field: n.field,
                opt: n.opt,
                value: n.value,
                end_braces: n.end_braces
            }
        });
        var adv_data = tempData.map(function (n) {
            return n
        });

        if (window.qtipApi) {
            window.qtipApi.hide();
            $('#btn-save_adv_filter').qtip('api').toggle(false);
        }

        if (is_empty(temp)) {
            notify_warn('提示', '查询条件为空!');
            return;
        }
        var queryData = JSON.stringify(temp);
        IssueMain.prototype.fetchIssuesByAdvQueryIssue(queryData, null, adv_data);
    });

});
</script>